<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "200px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(this.chartData);
    },

    setOptions(recordData) {
      console.log("recordData", recordData);

      /* const xAxisData = [
        "周一",
        "周二",
        "周三",
        "周四",
        "周五",
        "周六",
        "周日",
      ]; */
      this.chart.setOption({
        backgroundColor: "#fff",
        grid: {
          left: "10%",
          top: "20%",
          bottom: "10%",
          right: "7%",
        },
        tooltip: {
          trigger: "axis",
        },
        xAxis: {
          data: recordData.map((item) => {
            return item.date;
          }),
          axisTick: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "rgba(24, 144, 255, 0.1)",
              width: 1,
            },
          },
          axisLabel: {
            textStyle: {
              color: "#999",
              fontSize: 12,
            },
          },
          axisPointer: {
            type: "shadow",
          },
        },
        yAxis: [
          {
            name: "风险藏品数",
            nameTextStyle: {
              color: "#999",
              lineHeight: 30,
            },
            splitNumber: 4,
            axisTick: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: "rgba(24, 144, 255, 0.1)",
                width: 1,
              },
            },
            axisLabel: {
              textStyle: {
                color: "#999",
              },
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(24, 144, 255, 0.1)",
                width: 0.5,
                type: "dashed",
              },
            },
          },
        ],
        series: [
          {
            name: "新增风险",
            type: "pictorialBar",
            barCategoryGap: "0%",
            symbol:
              "path://M0,10 L10,10 C5.5,10 5.5,5 5,0 C4.5,5 4.5,10 0,10 z",
            itemStyle: {
              normal: {
                color: {
                  type: "linear",
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: "rgba(24, 144, 255, .8)", //  0%  处的颜色
                    },
                    {
                      offset: 1,
                      color: "rgba(24, 144, 255, .1)", //  100%  处的颜色
                    },
                  ],
                  global: false, //  缺省为  false
                },
              },
              emphasis: {
                opacity: 1,
              },
            },
            data: recordData.map((item) => {
              return item.num;
            }),
            z: 10,
          },
        ],
        animationEasing: "elasticOut",
        animationEasingUpdate: "elasticOut",
        animationDelay(idx) {
          return idx * 100;
        },
        animationDelayUpdate(idx) {
          return idx * 100;
        },
      });
    },
  },
};
</script>
